{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pythreejs import *\n",
    "from IPython.display import display\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BufferAttribute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = BufferAttribute(\n",
    "    array=[\n",
    "        [11, 12, 13],\n",
    "        [21, 22, 23],\n",
    "        [31, 32, 33],\n",
    "    ],\n",
    "    normalized=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.array = [ \n",
    "    [11, 12, 13],\n",
    "    [21,  0, 23],\n",
    "    [31, 32, 33],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# should be able to modify elements in the array directly \n",
    "# but currently, change events are not fired when arrays are \n",
    "# updated using element-wise assignment\n",
    "# WON'T WORK\n",
    "b.array[0, 1] = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BufferGeometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scale_factor = 20\n",
    "# Simple square\n",
    "vertices = BufferAttribute(\n",
    "    array=scale_factor * np.array([\n",
    "        [-1.0, -1.0,  0.0],\n",
    "        [ 1.0, -1.0,  0.0],\n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        \n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        [-1.0,  1.0,  0.0],\n",
    "        [-1.0, -1.0,  0.0],\n",
    "    ], dtype=np.float32),\n",
    "    normalized=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geometry = BufferGeometry(\n",
    "    attributes={'position': vertices},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vertices.array = scale_factor * np.array([\n",
    "        [-1.0, -1.0,  0.0],\n",
    "        [ 2.0, -2.0,  1.0],\n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        \n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        [-1.0,  1.0,  0.0],\n",
    "        [-1.0, -1.0,  0.0],\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vertices.array = scale_factor * np.array([\n",
    "        [-1.0, -1.0,  0.0],\n",
    "        [ 1.0, -1.0,  0.0],\n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        \n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        [-1.0,  1.0,  0.0],\n",
    "        [-1.0, -1.0,  0.0],\n",
    "    ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(repr(geometry))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Index attribute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simple square with unique vertices\n",
    "vertices = BufferAttribute(\n",
    "    array=scale_factor * np.array([\n",
    "        [-1.0, -1.0,  0.0],\n",
    "        [ 1.0, -1.0,  0.0],\n",
    "        [ 1.0,  1.0,  0.0],\n",
    "        [-1.0,  1.0,  0.0],\n",
    "    ], dtype=np.float32),\n",
    "    normalized=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Index buffer\n",
    "index = BufferAttribute(\n",
    "    array=np.array([\n",
    "        [0, 1, 2],\n",
    "        [2, 3, 0],\n",
    "    ], dtype=np.uint16).ravel(),\n",
    "    normalized=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geometry = BufferGeometry(\n",
    "    attributes={\n",
    "        'position': vertices,\n",
    "        'index': index,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geometry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PlainBufferGeometry.from_geometry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sphere = SphereBufferGeometry(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(repr(sphere))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plain = BufferGeometry.from_geometry(sphere)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now from a non-buffer geometry:\n",
    "box = BufferGeometry.from_geometry(BoxGeometry())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "box"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This shows that when converting from a regular geometry\n",
    "# to a buffer geometry, it does not use indexing (position attribute\n",
    "# is expanded):\n",
    "box.attributes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
